<?

require_once 'global_variable_replacer.php'; // NEW

$plugin_info = array(
  'pi_name' => 'Page title',
  'pi_version' => '0.1',
  'pi_author' => 'Tom Kiss',
  'pi_author_url' => 'http://eu.ncsoft.com',
  'pi_description' => 'Creates and saves a CSS background image for the page title on most TR pages.',
  'pi_usage' => Pagetitle::usage()
  );

class Pagetitle {

	var $return_data = "";

	function numname() {
		return rand(500, 1000);
	}

	function Pagetitle() {
		global $TMPL;
		$text = $TMPL->tagdata;
		
		$replacer = new Global_variable_replacer(); // NEW
		$text = $replacer->getReplacedText($text); // NEW
		$this->lang = $replacer->getReplacedText("{language}"); // NEW
		
    	$this->link = (!$TMPL->fetch_param('link')) ? $this->numname() : $TMPL->fetch_param('link');
    	$this->type = (!$TMPL->fetch_param('type')) ? '' : $TMPL->fetch_param('type');
		if (!empty($text)  && !empty($this->type)) {
			$text = html_entity_decode($text);// convert HTML entities  to normal
			$text = str_replace("&#8217;", "'", $text);

//			print("<!-- text: $text -->\n");//TMP
			$arrRes = $this->savePagetitleImage($text);
			if (!empty($link)) {
				$linky = array('<a href="'.$this->link.'" style="height: '.$arrRes[1].'px">', '</a>');
			} else {
				$linky = array('', '');
			}
			switch ($this->type) {
				case "sub":
					$this->return_data = '<h2 style="background-repeat: no-repeat; background-image: url('.$arrRes[0].'); height: '.$arrRes[1].'px">'.$linky[0].'<span class="hide">'.$text.'</span>'.$linky[1].'</h2>';
				break;
				case "cat":
					$this->return_data = '<h3 style="background-repeat: no-repeat; background-image: url('.$arrRes[0].'); height: '.$arrRes[1].'px">'.$linky[0].'<span class="hide">'.$text.'</span>'.$linky[1].'</h3>';
				break;
				case "main":
					$this->return_data = '<h1 style="background-repeat: no-repeat; background-image: url('.$arrRes[0].'); height: '.$arrRes[1].'px">'.$linky[0].'<span class="hide">'.$text.'</span>'.$linky[1].'</h1>';
				break;
			}
		} else {
			$this->return_data = '';
		}
	}
	
	function doTheThing($mixed) {
		$mixed = str_replace("&AMP;", "&", $mixed);
	   	return $mixed;
	}

	function savePagetitleImage($string) {
		global $TMPL;
		// Check to see if this image exists, if it does not, then create it

		$filename = strtolower(str_replace(" ", "", $string));
		$filename = '/images/assets/'.$this->lang.'/pagetitles/'.md5($filename.$this->type).'.png';
				
//		$string = mb_strtoupper($string, "utf-8");
		$string = $this->doTheThing($string);
		$stringArr='';
				
		// check width is the same
		if (!file_exists($_SERVER['DOCUMENT_ROOT'].$filename)) {
			// File settings
			
			
			$fontfile = $_SERVER['DOCUMENT_ROOT']."/images/assets/shared/pagetitles/GothamRnd-Book.otf";
			
			if ($this->type == "sub") {
				$toppadding = 0;
				$leftpadding = 0;
				$rightpadding = 0;
				$bottompadding = 5;
				$width = 680;
				$height = 20;
				$fsize = 16;
				$lineheight = (ceil($fsize/5));
				$fontcolour = "00eaff";
				$backcolour = "000000";
				$beveltop = "";
				$bevelbottom = "";
			} else if ($this->type == "cat") {
				$toppadding = 0;
				$leftpadding = 0;
				$rightpadding = 0;
				$bottompadding = 5;
				$width = 680;
				$height = 20;
				$fsize = 13;
				$lineheight = (ceil($fsize/6));
				$fontcolour = "00eaff";
				$backcolour = "000000";
				$beveltop = "";
				$bevelbottom = "";
			} else {
				$toppadding = 0;
				$leftpadding = 0;
				$rightpadding = 0;
				$bottompadding = 5;
				$width = 720;
				$height = 21;
				$fsize = 19;
				$lineheight = (ceil($fsize/4));
				$fontcolour = "00eaff";
				$backcolour = "000000";
				$beveltop = "";
				$bevelbottom = "";
			}
			
			// Get querystring
			if (empty($string)) {
				$string = 'ERROR :) ';
			}
			
			// Find out size of text box
			$fontsize = imagettfbbox($fsize,0,$fontfile,$string);
			$textboxheight = abs($fontsize[7]);
			$textboxwidth = $fontsize[4]-$fontsize[6];
			// Re format text if it breaks the boundary
			if ($textboxwidth > $width) {
				$pc = $textboxwidth/($width-$leftpadding-$rightpadding);
				$len = strlen($string)/$pc;
				$string = wordwrap($string, $len, '~');
				$stringArr = split('~', $string);
				if ($textboxheight < $fsize) {
					$textboxheight = $fsize;
				}
				$textboxheight = (($textboxheight)*sizeof($stringArr)) + ($lineheight*(sizeof($stringArr)-1));
			} else {
				if ($textboxheight < $fsize) {
					$textboxheight = $fsize;
				}
			}
			// Create image
			$imageheight = $textboxheight+$toppadding+$bottompadding;
			
			//$im = imagecreate($width,$imageheight);
			$im = imagecreate($width,$imageheight);
			
			$forered1 = hexdec(substr($fontcolour,0,2));
			$foregreen1 = hexdec(substr($fontcolour,2,2));
			$foreblue1 = hexdec(substr($fontcolour,4,2));
			$backcolourred = hexdec(substr($backcolour,0,2));
			$backcolourgreen = hexdec(substr($backcolour,2,2));
			$backcolourblue = hexdec(substr($backcolour,4,2));
			$backcolour1 = imagecolorallocate($im, $backcolourred, $backcolourgreen, $backcolourblue);
			$textcolour1 = imagecolorallocate($im, $forered1, $foregreen1, $foreblue1);
			
			if ($this->type != "sub") {
				$beveltopred = hexdec(substr($beveltop,0,2));
				$beveltopgreen = hexdec(substr($beveltop,2,2));
				$beveltopblue = hexdec(substr($beveltop,4,2));
				$bevelbottomred = hexdec(substr($bevelbottom,0,2));
				$bevelbottomgreen = hexdec(substr($bevelbottom,2,2));
				$bevelbottomblue = hexdec(substr($bevelbottom,4,2));
				$beveltop1 = imagecolorallocate($im, $beveltopred, $beveltopgreen, $beveltopblue);
				$bevelbottom1 = imagecolorallocate($im, $bevelbottomred, $bevelbottomgreen, $bevelbottomblue);
			}
			
			imagecolortransparent($im, $backcolour1);
			imagealphablending($im, false);
						
			// Insert text
			if (is_array($stringArr)) {
				for ($i=0; $i<sizeof($stringArr); $i++) {

					$dist = ($toppadding+(($fsize+$lineheight)*$i))+$fsize;
					if ($this->type != "sub") {
						// bevel 
						imagettftext($im,$fsize,0,$leftpadding-1,$dist-1,$beveltop1,$fontfile,$stringArr[$i]);
						imagettftext($im,$fsize,0,$leftpadding+1,$dist+1,$bevelbottom1,$fontfile,$stringArr[$i]);
					}
					// text
					imagettftext($im,$fsize,0,$leftpadding,$dist,$textcolour1,$fontfile,$stringArr[$i]);
				}
			} else {
				if ($this->type != "sub") {
					// bevel 
					imagettftext($im,$fsize,0,$leftpadding-1,$toppadding+$fsize,$beveltop1,$fontfile,$string);
					imagettftext($im,$fsize,0,$leftpadding+1,$toppadding+$fsize+1,$bevelbottom1,$fontfile,$string);
				}
				// text
				imagettftext($im,$fsize,0,$leftpadding,$toppadding+$fsize,$textcolour1,$fontfile,$string);
			}
										
			
			// Output image
			imagepng($im, $_SERVER['DOCUMENT_ROOT'].$filename); 
		} else {
			$imgdata = getimagesize($_SERVER['DOCUMENT_ROOT'].$filename);
			$width = $imgdata[0];
			$imageheight = $imgdata[1];
		}
		return array($filename, $imageheight);
	}	
	

	function usage() {
		ob_start(); 
  ?>
  
This plugin will simply return the inline style for page titles

{exp:pagetitle}The Page Title{/exp:pagetitle}
<?
	  $buffer = ob_get_contents();
	  ob_end_clean(); 
	  return $buffer;
	}
}
?>
